package com.zcl.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sun.istack.internal.logging.Logger;
import com.zcl.dao.Help;
import com.zcl.service.HelpService;
import com.zcl.util.GetCurrentUserDetails;

@Controller
public class HelpController {
	Logger logger = Logger.getLogger(HelpController.class);
	@Autowired
	HelpService helpService;
	/**
	 * 处理	/help
	 * @return	船舶信息页面
	 */
	@RequestMapping("/help")
	public String helpIndex() {
		return "help";
	}
	
	/**
	 * 事故信息概况查询
	 */
	@ResponseBody
	@RequestMapping("/help/surveryQuery")
	public List<Map<String, String>> helpQuery(Model model) {
		List<Map<String, String>> helpList = helpService.helpQuery();
		return helpList;
	}
	
	/**
	 * 事故信息概况-操作-详情
	 * @return
	 */
	@RequestMapping("/helpSurveryInfo")
	public String helpSuveryInfo(@RequestParam("accidentId") String accidentId,Model model) {
		Map<String, String> map = new HashMap<String,String>();
		map.put("accidentId", accidentId);
		Help help = helpService.helpSuveryInfo(map);
		model.addAttribute("help", help);
		System.out.println("事故信息概况-操作-详情:accidentId="+accidentId);
		return "help_information";
	}
	
	/**
	 * 事故信息概况-操作-编辑
	 * @return
	 */
	@RequestMapping("/helpSurveryRevise")
	public String helpSuveryRevise(@RequestParam("accidentId") String accidentId,Model model) {
		Map<String, String> map = new HashMap<String,String>();
		map.put("accidentId", accidentId);
		Help help = helpService.helpSuveryInfo(map);
		model.addAttribute("help", help);
		System.out.println("事故信息概况-操作-编辑:accidentId="+accidentId);
		return "help_revise";
	}
	
	/**
	 * 事故信息概况-操作-编辑2--获取某些下拉框值
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/helpSurveryRevise2")
	public Help helpSuveryRevise2(@RequestParam("accidentId") String accidentId,Model model) {
		Map<String, String> map = new HashMap<String,String>();
		map.put("accidentId", accidentId);
		Help help = helpService.helpSuveryInfo(map);
		System.out.println("事故信息概况-操作-编辑2--获取某些下拉框值:accidentId="+accidentId);
		return help;
	}
	
	/**
	 * 事故信息概况-操作-删除
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/help/delete")
	public void helpDelete(@RequestParam("accidentId") String accidentId) {
		Map<String, String> map = new HashMap<String,String>();
		map.put("accidentId", accidentId);
		helpService.helpDelete(map);
		System.out.println("删除");;
	}
	
	/**
	 * 事故添加页面
	 * @return
	 */
	@RequestMapping("/helpAdd")
	public String helphelpAdd() {
		return "help_add";
	}
	
	/**
	 * 事故信息查询-全文搜索
	 * @param search_input
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/help/globalQuery")
	public List<Map<String, String>> helpGlobalQuery(@RequestParam("search_input") String search_input){
		Map<String, String> map = new HashMap<String,String>();
		map.put("search_input", search_input);
		List<Map<String, String>> helpList = helpService.helpGlobalQuery(map);
		return helpList;
		
	} 
	
	/**
	 * 事故信息查询-高级搜索
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/help/advancedQuery")
	public List<Map<String, String>> helpAdvancedQuery(HttpServletRequest request){
		Map<String, String> map = new HashMap<String,String>();
		
		map.put("accidentId", request.getParameter("accidentId"));
		map.put("accidentName", request.getParameter("accidentName"));
		map.put("timely", request.getParameter("timely"));
		map.put("successly", request.getParameter("successly"));
		map.put("helpService", request.getParameter("helpService"));
		map.put("helpBoat", request.getParameter("helpBoat"));
		map.put("loss", request.getParameter("loss"));
		List<Map<String, String>> helpList = helpService.helpAdvancedQuery(map);
		return helpList;
		
	} 
	
	/**
	 * 事故信息录入
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping("/help/addHelp")
	public void helpAdd(HttpServletRequest request) throws UnsupportedEncodingException {
		String accidentId = UUID.randomUUID().toString();
		Map<String, String> map = new HashMap<String,String>();
		map.put("accidentId", accidentId);
		map.put("accidentName", request.getParameter("accidentName"));
		map.put("timely", request.getParameter("timely"));
		map.put("successly", request.getParameter("successly"));
		map.put("helpService", request.getParameter("helpService"));
		map.put("helpBoat", request.getParameter("helpBoat"));
		map.put("loss", request.getParameter("loss"));
		logger.info("事故信息录入");
		helpService.helpAdd(map);
	}
	
	/**
	 * 事故信息修改
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping("/help/reviseHelp")
	public void helpRevise(HttpServletRequest request) throws UnsupportedEncodingException {
		
		Map<String, String> map = new HashMap<String,String>();
		map.put("accidentId", request.getParameter("accidentId"));
		map.put("accidentName", request.getParameter("accidentName"));
		map.put("timely", request.getParameter("timely"));
		map.put("successly", request.getParameter("successly"));
		map.put("helpService", request.getParameter("helpService"));
		map.put("helpBoat", request.getParameter("helpBoat"));
		map.put("loss", request.getParameter("loss"));
		System.out.println("helpRevise");
		helpService.helpRevise(map);
	}
}
